using System;
using System.Drawing;
using System.Windows.Forms;
using HuaRongDaoGame.Models;

namespace HuaRongDaoGame.Forms
{
    /// <summary>
    /// 游戏统计信息窗体
    /// </summary>
    public partial class StatisticsForm : Form
    {
        private GameStatistics statistics;

        /// <summary>
        /// 构造函数
        /// </summary>
        public StatisticsForm(GameStatistics statistics)
        {
            this.statistics = statistics;
            InitializeStatisticsForm();
        }

        /// <summary>
        /// 初始化统计窗体
        /// </summary>
        private void InitializeStatisticsForm()
        {
            this.Text = "游戏统计";
            this.Size = new Size(500, 400);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;

            CreateControls();
        }

        /// <summary>
        /// 创建控件
        /// </summary>
        private void CreateControls()
        {
            // 标题标签
            var titleLabel = new Label
            {
                Text = "游戏统计信息",
                Font = new Font("Microsoft YaHei", 16, FontStyle.Bold),
                ForeColor = Color.DarkBlue,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                Height = 40
            };

            // 创建统计信息面板
            var statsPanel = new Panel
            {
                Dock = DockStyle.Fill,
                Padding = new Padding(20),
                AutoScroll = true
            };

            // 统计信息标签
            var statsLabels = CreateStatisticsLabels();
            foreach (var label in statsLabels)
            {
                statsPanel.Controls.Add(label);
            }

            // 计算按钮位置
            int buttonY = statsLabels.Length * 35 + 40;

            // 重置按钮
            var resetButton = new Button
            {
                Text = "重置统计",
                Size = new Size(100, 30),
                Location = new Point(20, buttonY),
                BackColor = Color.LightCoral,
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat
            };
            resetButton.Click += ResetButton_Click;

            // 关闭按钮
            var closeButton = new Button
            {
                Text = "关闭",
                Size = new Size(100, 30),
                Location = new Point(140, buttonY),
                BackColor = Color.LightGray,
                FlatStyle = FlatStyle.Flat
            };
            closeButton.Click += (s, e) => this.Close();

            // 添加控件到窗体
            this.Controls.Add(titleLabel);
            this.Controls.Add(statsPanel);
            statsPanel.Controls.Add(resetButton);
            statsPanel.Controls.Add(closeButton);
        }

        /// <summary>
        /// 创建统计信息标签
        /// </summary>
        private Label[] CreateStatisticsLabels()
        {
            var labels = new Label[8];
            int y = 20;

            // 总游戏次数
            labels[0] = CreateStatLabel("总游戏次数:", statistics.TotalGamesPlayed.ToString(), y);
            y += 35;

            // 胜利次数
            labels[1] = CreateStatLabel("胜利次数:", statistics.TotalGamesWon.ToString(), y);
            y += 35;

            // 胜率
            labels[2] = CreateStatLabel("胜率:", string.Format("{0:P1}", statistics.WinRate), y);
            y += 35;

            // 总步数
            labels[3] = CreateStatLabel("总步数:", statistics.TotalMoves.ToString(), y);
            y += 35;

            // 平均步数
            labels[4] = CreateStatLabel("平均步数:", statistics.AverageMoves.ToString(), y);
            y += 35;

            // 最佳时间
            labels[5] = CreateStatLabel("最佳时间:", FormatTimeSpan(statistics.BestTime), y);
            y += 35;

            // 最少步数
            labels[6] = CreateStatLabel("最少步数:", statistics.BestMoves > 0 ? statistics.BestMoves.ToString() : "无记录", y);
            y += 35;

            // 平均时间
            var avgTime = statistics.TotalGamesPlayed > 0 ? 
                TimeSpan.FromTicks(statistics.TotalTime.Ticks / statistics.TotalGamesPlayed) : TimeSpan.Zero;
            labels[7] = CreateStatLabel("平均时间:", FormatTimeSpan(avgTime), y);

            return labels;
        }

        /// <summary>
        /// 创建统计标签
        /// </summary>
        private Label CreateStatLabel(string title, string value, int y)
        {
            var label = new Label
            {
                Text = string.Format("{0} {1}", title, value),
                Font = new Font("Microsoft YaHei", 12),
                ForeColor = Color.DarkGreen,
                Location = new Point(20, y),
                Size = new Size(400, 25),
                TextAlign = ContentAlignment.MiddleLeft
            };

            return label;
        }

        /// <summary>
        /// 格式化时间
        /// </summary>
        private string FormatTimeSpan(TimeSpan time)
        {
            if (time == TimeSpan.Zero)
                return "无记录";

            if (time.TotalHours >= 1)
            {
                return string.Format("{0:D2}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
            }
            else
            {
                return string.Format("{0:D2}:{1:D2}", time.Minutes, time.Seconds);
            }
        }

        /// <summary>
        /// 重置按钮点击事件
        /// </summary>
        private void ResetButton_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show(
                "确定要重置所有游戏统计信息吗？此操作不可撤销。",
                "确认重置",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            if (result == DialogResult.Yes)
            {
                ResetStatistics();
                MessageBox.Show("统计信息已重置！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        /// <summary>
        /// 重置统计信息
        /// </summary>
        private void ResetStatistics()
        {
            statistics.TotalGamesPlayed = 0;
            statistics.TotalGamesWon = 0;
            statistics.TotalMoves = 0;
            statistics.AverageMoves = 0;
            statistics.BestTime = TimeSpan.Zero;
            statistics.BestMoves = 0;
            statistics.TotalTime = TimeSpan.Zero;

            // 保存重置后的统计信息
            try
            {
                HuaRongDaoGame.Services.GameDataService.SaveStatistics(statistics);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("保存统计信息时出错：{0}", ex.Message), 
                    "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
} 